/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.artezio.buyndrive.web;

import com.artezio.buyndrive.domain.Carservice;
import com.artezio.buyndrive.domain.Turn;
import com.artezio.buyndrive.domain.Role;
import com.artezio.buyndrive.domain.Users;
import com.artezio.buyndrive.service.CarserviceService;
import com.artezio.buyndrive.service.CityService;
import com.artezio.buyndrive.service.SectionTurnService;
import com.artezio.buyndrive.service.TurnService;
import com.artezio.buyndrive.validator.TurnValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 *
 * @author vgordievich
 */
@Controller
@RequestMapping("/carservicePage/turn")
public class TurnController {

    @Autowired
    private CarserviceService carserviceService;
    @Autowired
    private TurnService turnService;
    @Autowired
    private CityService cityService;
    @Autowired
    private SectionTurnService sectionTurnService;
    @Autowired
    private TurnValidator TurnValidator;

    @RequestMapping("/list/{id}")
    public ModelAndView begin(@PathVariable("id") Long id, HttpSession httpSession) {
       // Users users = (Users) httpSession.getAttribute("user");
        
        httpSession.setAttribute("carserviceForListTurn", carserviceService.findById(id));

        return new ModelAndView("redirect:/carservicePage/turn/list");
    }
    
    @RequestMapping("/list")
    public ModelAndView list(Model model, HttpSession httpSession) {
       // Users users = (Users) httpSession.getAttribute("user");
        
        Carservice carservice = (Carservice) httpSession.getAttribute("carserviceForListTurn");
        model.addAttribute("listTurn", turnService.getByCarserviceAndSection(carservice, null));
        model.addAttribute("turn", new Turn());
        model.addAttribute("carserviceName", carservice.getName());

        return new ModelAndView("carservicePage");
    }

    @RequestMapping("/edit/{id}")
    public ModelAndView details(@PathVariable("id") Long idTurn,
            Model model, HttpSession httpSession) {

        Turn turn = turnService.findById(idTurn);
        model.addAttribute("turn", turnService.findById(idTurn));

        httpSession.setAttribute("turn", turn);
        model.addAttribute("sectionList", sectionTurnService.getAll());
        
        model.addAttribute("carserviceName", 
                ((Carservice) httpSession.getAttribute("carserviceForListTurn"))
                .getName());

        return new ModelAndView("turnDetails");
    }

    @RequestMapping("/toInsert")
    public ModelAndView insert(Model model, HttpSession httpSession) {

        model.addAttribute("user", new Users());

        model.addAttribute("turn", new Turn());
        httpSession.setAttribute("turn", null);
        model.addAttribute("sectionList", sectionTurnService.getAll());

        model.addAttribute("carserviceName", 
                ((Carservice) httpSession.getAttribute("carserviceForListTurn"))
                .getName());
        
        return new ModelAndView("turnDetails");
    }

    @RequestMapping("/save")
    public ModelAndView save(Turn turn, BindingResult result,
            Model model, HttpSession httpSession) {

        Users users = (Users) httpSession.getAttribute("user");

        TurnValidator.validate(turn, result);

        if (!result.hasErrors()) {
            Turn turnSave = (Turn) httpSession.getAttribute("turn");

            turn.setIdCarservice(carserviceService
                    .findById((
                    (Carservice) httpSession
                    .getAttribute("carserviceForListTurn")).getId()));
            if (turnSave != null) {
                turn.setId(turnSave.getId());
                turnService.saveOrUpdate(turn);
            } else {
                turnService.add(turn);
            }
            httpSession.setAttribute("turn", turn);
            return new ModelAndView("redirect:/carservicePage/turn/list");
        }

        model.addAttribute("sectionList", sectionTurnService.getAll());

        model.addAttribute("turn", turn);
        return new ModelAndView("turnDetails");

    }

    @RequestMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable("id") Long idTurn,
            Model model, HttpSession httpSession) {
        turnService.delete(idTurn);


        return new ModelAndView("redirect:/carservicePage/turn/list");
    }
   
}
